package com.sc.nft.dao;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.entity.EquityProps;
import com.sc.nft.entity.dto.MerchantAvailabilityListDTO;
import com.sc.nft.entity.dto.VistInFlagUserDTO;
import com.sc.nft.entity.vo.*;
import com.sc.nft.entity.vo.secondEquityProp.EquityPropsLastConsignDetailsVO;
import com.sc.nft.enums.EquityPropsTypeEnum;
import com.sc.nft.mapper.EquityPropsMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.List;

/**
 * 权益道具表(sc_equity_props)数据DAO
 *
 * @author Sun
 * @since 2023-03-28 10:29:22
 * @description 由 Mybatisplus Code Generator 创建
 */
@Slf4j
@Repository
public class EquityPropsDao extends ServiceImpl<EquityPropsMapper, EquityProps> {

    public Page<PageAdminEquityPropsVO> getPageAdminEquityProps(String firstTitle, Boolean status, Boolean showFlag, Integer pageNo, Integer pageSize) {
        return baseMapper.getPageAdminEquityProps(new Page<>(pageNo, pageSize), firstTitle, status, showFlag);
    }

    public Boolean updateStatusById(Long id, Boolean status) {
        UpdateWrapper<EquityProps> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", id);
        wrapper.set("status", status);
        return update(new EquityProps(), wrapper);
    }

    public Page<ProdctAssociateEquityPropsVO> prodctAssociateEquityProps(Page page, String firstTitle) {
        return baseMapper.prodctAssociateEquityProps(page, firstTitle);
    }

    public Page<AirdropGrantEquityPropsVO> airdropGrantEquityProps(Page page, String firstTitle, Integer typeClassify) {
        return baseMapper.airdropGrantEquityProps(page, firstTitle, typeClassify);
    }


    public Page<EquityProps> getDaoUseList(Page page) {
        LambdaQueryWrapper<EquityProps> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EquityProps::getStatus, true);
        wrapper.eq(EquityProps::getIsBrandUse, true);
        return page(page, wrapper);
    }

    public boolean updateShowFlagById(Long id, Boolean showFlag) {
        LambdaUpdateWrapper<EquityProps> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(EquityProps::getId, id);
        wrapper.set(EquityProps::getShowFlag, showFlag);
        return update(new EquityProps(), wrapper);
    }

    public Page<EquityProps> simpleMerchantAvailabilityList(MerchantAvailabilityListDTO merchantAvailabilityListDTO) {
        LambdaQueryWrapper<EquityProps> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(EquityProps::getIsBrandUse, true);
        lambdaQueryWrapper.eq(EquityProps::getStatus, true);
        lambdaQueryWrapper.eq(merchantAvailabilityListDTO.getType() != null, EquityProps::getTypeClassify, merchantAvailabilityListDTO.getType());
        lambdaQueryWrapper.like(StrUtil.isNotBlank(merchantAvailabilityListDTO.getFirstTitle()), EquityProps::getFirstTitle, merchantAvailabilityListDTO.getFirstTitle());
        return page(merchantAvailabilityListDTO.toMp(), lambdaQueryWrapper);
    }

    public Page<MerchantAvailabilityListVO> simpleAvailabilityList(MerchantAvailabilityListDTO merchantAvailabilityListDTO) {
        LambdaQueryWrapper<EquityProps> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(EquityProps::getStatus, true);
        lambdaQueryWrapper.eq(EquityProps::getTypeClassify, merchantAvailabilityListDTO.getType());
        lambdaQueryWrapper.like(StrUtil.isNotBlank(merchantAvailabilityListDTO.getFirstTitle()), EquityProps::getFirstTitle, merchantAvailabilityListDTO.getFirstTitle());
        return page(merchantAvailabilityListDTO.toMp(), lambdaQueryWrapper);
    }


    public Integer getPropIdsAndTypeClassifyCount(List<Long> propIds) {
        LambdaQueryWrapper<EquityProps> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.in(EquityProps::getId, propIds);
        lambdaQueryWrapper.eq(EquityProps::getTypeClassify, EquityPropsTypeEnum.VOUCHER);
        return count(lambdaQueryWrapper);
    }

    public Page<MerchantAvailabilityListVO> equityPropAllPage(Page page) {
        LambdaQueryWrapper<EquityProps> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(EquityProps::getStatus, true);
        lambdaQueryWrapper.in(EquityProps::getTypeClassify, EquityPropsTypeEnum.VIRTUAL_PROPS, EquityPropsTypeEnum.VOUCHER, EquityPropsTypeEnum.RAFFLE_TICKET);
        return page(page, lambdaQueryWrapper);
    }

    public EquityProps getByFirstTitle(String firstTitle) {
        LambdaQueryWrapper<EquityProps> query = new LambdaQueryWrapper<>();
        query.eq(EquityProps::getFirstTitle, firstTitle);
        query.eq(EquityProps::getIsDelete, Boolean.FALSE);
        query.last("LIMIT 1");
        return getOne(query);
    }

    public Boolean deleteEquityProps(Long id) {
        LambdaUpdateWrapper<EquityProps> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(EquityProps::getId, id);
        return remove(wrapper);
    }

    public Page<MerchantAvailabilityListVO> getLongTermEquityPropPage(Page page, String firstTitle, Integer type) {
        return baseMapper.getLongTermEquityPropPage(page, firstTitle, type);
    }

    public EquityPropsLastConsignDetailsVO getLastConsignDetails(Long equityPropsId) {
        return baseMapper.getLastConsignDetails(equityPropsId);
    }

    public void updateNewDealPrice(Long id, BigDecimal newDealPrice) {
        baseMapper.updateNewDealPriceById(id, newDealPrice);
    }

    /**
     * 获取商城上架中的权益道具
     * @return
     */
    public Page<MallEquityPropsVO> getMallEquityProps(Integer pageNo, Integer pageSize, Long flag,String firstTitle) {
        return baseMapper.getMallEquityProps(new Page<>(pageNo, pageSize), flag, firstTitle);
    }

    public List<EquityProps> getWithDao(Long daoId){
        LambdaQueryWrapper<EquityProps> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(EquityProps::getDaoId,daoId);
        queryWrapper.eq(EquityProps::getStatus,Boolean.TRUE);
        return list(queryWrapper);
    }
}